package com.zhuiyun.project.api.caseImage.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhuiyun.project.api.caseImage.entity.CaseImage;
import com.zhuiyun.project.api.caseImage.mapper.CaseImageMapper;
import com.zhuiyun.project.api.caseImage.service.ICaseImageService;
import com.zhuiyun.project.api.coachmanagement.entity.CoachManagement;
import com.zhuiyun.project.api.coachmanagement.mapper.CoachManagementMapper;
import com.zhuiyun.project.api.memberUserManagement.entity.MemberUserManagement;
import com.zhuiyun.project.api.memberUserManagement.mapper.MemberUserManagementMapper;
import com.zhuiyun.project.api.plan.fitnessPlan.entity.FitnessPlan;
import com.zhuiyun.project.api.plan.fitnessPlan.mapper.FitnessPlanMapper;
import com.zhuiyun.project.api.reservation.entity.Reservation;
import com.zhuiyun.project.api.reservation.mapper.ReservationMapper;
import com.zhuiyun.project.api.userrecord.entity.UserRecord;
import com.zhuiyun.project.api.userrecord.mapper.UserRecordMapper;
import com.zhuiyun.project.api.vipPlan.entity.MemberPlan;
import com.zhuiyun.project.api.vipPlan.mapper.VipPlanMapper;
import com.zhuiyun.project.common.response.CommonResult;
import com.zhuiyun.project.utils.pageUtil.MybatisPageHelper;
import com.zhuiyun.project.utils.pageUtil.PageRequest;
import com.zhuiyun.project.utils.pageUtil.PageResult;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName CaseImageController
 * @Description TODO
 * @Author xy
 * @Date 2023/12/11 19:25
 **/
@RestController
@Api(tags = "案例管理")
@RequestMapping("/caseImage")
@Slf4j
public class CaseImageController {
    @Resource
    CaseImageMapper caseImageMapper;
    @Resource
    ICaseImageService caseImageService;
    @Resource
    UserRecordMapper userRecordMapper;
    @Resource
    ReservationMapper reservationMapper;
    @Resource
    VipPlanMapper vipPlanMapper;
    @Resource
    FitnessPlanMapper fitnessPlanMapper;
    @Resource
    MemberUserManagementMapper memberUserManagementMapper;
    @Resource
    CoachManagementMapper coachManagementMapper;


    /*
    * @Author xy
    * @Description todo 案例查询
    * @Date 2023/12/11 19:39
    * @Return
    * @param
    */
    @RequestMapping("/selectCaseImage")
    public CommonResult selectCaseImage(@RequestParam(defaultValue = "") String coachName,PageRequest pageRequest){
        MybatisPageHelper.startPage(pageRequest);
        QueryWrapper<CaseImage> queryWrapper = new QueryWrapper<>();
        if (!"".equals(coachName)) {
            // 获取匹配的教练ID列表
            QueryWrapper<CoachManagement> coachQueryWrapper = new QueryWrapper<>();
            coachQueryWrapper.like("coach_name", coachName);
            List<CoachManagement> coachManagements = coachManagementMapper.selectList(coachQueryWrapper);
            List<Integer> coachIds = coachManagements.stream().map(CoachManagement::getId).collect(Collectors.toList());

            if (!coachIds.isEmpty()) {
                // 在CaseImage表中使用教练ID进行模糊搜索
                queryWrapper.in("coach_id", coachIds);
            } else {
                // 如果没有匹配的教练，则不会有匹配的CaseImage
                return CommonResult.ok(Collections.emptyList());
            }
        }

        List<CaseImage> caseImages = caseImageMapper.selectList(queryWrapper);

        for (CaseImage caseImage : caseImages) {
            Integer userId = caseImage.getUserId();
            Integer coachId = caseImage.getCoachId();

            // 根据教练id查询教练名称
            CoachManagement coachManagement = coachManagementMapper.selectById(coachId);
            caseImage.setCoachName(coachManagement.getCoachName());

            // 根据用户id查询学员姓名
            MemberUserManagement memberUserManagement = memberUserManagementMapper.selectById(userId);
            caseImage.setStudentName(memberUserManagement.getMemberUserRealName());

            // 根据用户id查询课程名称
            QueryWrapper<Reservation> reservationQueryWrapper = new QueryWrapper<>();
            reservationQueryWrapper.eq("student_id",userId);
            List<Reservation> reservations = reservationMapper.selectList(reservationQueryWrapper);
            for (Reservation reservation : reservations) {
                Integer courseId = reservation.getCourseId();
                MemberPlan memberPlan = vipPlanMapper.selectById(courseId);
                int planId = memberPlan.getPlanId();
                FitnessPlan fitnessPlan = fitnessPlanMapper.selectById(planId);
                caseImage.setCourseName(fitnessPlan.getFitnessPlanName());
            }
        }
        PageResult pageResult = MybatisPageHelper.getPageResult(caseImages);
        return CommonResult.ok(pageResult);
    }
    /*
     * @Author xy
     * @Description todo 根据用户id案例查询
     * @Date 2023/12/11 19:39
     * @Return
     * @param
     */
    @RequestMapping("/selectCaseImageByUserId")
    public CommonResult selectCaseImage(Integer userId){
        QueryWrapper<CaseImage> reservationQueryWrapper = new QueryWrapper<>();
        reservationQueryWrapper.eq("user_id",userId);
        List<CaseImage> caseImages = caseImageMapper.selectList(reservationQueryWrapper);

        return CommonResult.ok(caseImages);
    }
    /*
     * @Author xy
     * @Description todo 根据用户id案例查询
     * @Date 2023/12/11 19:39
     * @Return
     * @param
     */
    @RequestMapping("/selectCaseImageByTime")
    public CommonResult selectCaseImage(String time,Integer userId){
        QueryWrapper<CaseImage> reservationQueryWrapper = new QueryWrapper<>();
        reservationQueryWrapper.eq("create_time",time);
        reservationQueryWrapper.eq("user_id",userId);
        List<CaseImage> caseImages = caseImageMapper.selectList(reservationQueryWrapper);
        return CommonResult.ok(caseImages);
    }
}
